package com.atguigu.srb.core.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.BorrowerAttachMapper;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.mapper.UserIntegralMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.BorrowerAttach;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.UserIntegral;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author lupengning
 * @since 2021-05-27
 */
@Service
@Slf4j
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements
    BorrowerService {

  @Resource
  private BorrowerAttachMapper borrowerAttachMapper;

  @Resource
  private UserInfoMapper userInfoMapper;

  @Resource
  private UserIntegralMapper userIntegralMapper;

  @Resource
  private DictService dictService;

  @Resource
  private BorrowerAttachService borrowerAttachService;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {
    UserInfo userInfo = userInfoMapper.selectById(userId);

    //保存借款人信息
    Borrower borrower = new Borrower();
    BeanUtils.copyProperties(borrowerVO, borrower);
    borrower.setUserId(userId);
    borrower.setName(userInfo.getName());
    borrower.setIdCard(userInfo.getIdCard());
    borrower.setMobile(userInfo.getMobile());
    borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());//认证中
    baseMapper.insert(borrower);

    //保存附件
    List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
    borrowerAttachList.forEach(borrowerAttach -> {
      borrowerAttach.setBorrowerId(borrower.getId());
      borrowerAttachMapper.insert(borrowerAttach);
    });

    //更新会员状态，更新为认证中
    userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
    userInfoMapper.updateById(userInfo);
  }

  @Override
  public Integer getStatusByUserId(Long userId) {
    QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
    borrowerQueryWrapper.select("status").eq("user_id", userId);
    List<Object> objects = baseMapper.selectObjs(borrowerQueryWrapper);

    if (objects.size() == 0) {
      //借款人尚未提交信息
      return BorrowerStatusEnum.NO_AUTH.getStatus();
    }
    Integer status = (Integer) objects.get(0);
    return status;
  }

  @Override
  public IPage<Borrower> listPage(Page<Borrower> borrowerPage, String keyword) {
    if (StringUtil.isBlank(keyword)) {
      return baseMapper.selectPage(borrowerPage, null);
    }
    log.info("keyword", keyword);
    QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
    borrowerQueryWrapper.like("name", keyword)
        .or().like("id_card", keyword)
        .or().like("mobile", keyword)
        .orderByDesc("id");
    return baseMapper.selectPage(borrowerPage, borrowerQueryWrapper);
  }

  @Override
  public BorrowerDetailVO getBorrowerDetailVOById(Long id) {

    //获取借款人信息
    Borrower borrower = baseMapper.selectById(id);

    //填充借款人VO信息
    BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
    BeanUtils.copyProperties(borrower, borrowerDetailVO);

    //婚否
    borrowerDetailVO.setMarry(borrower.getMarry() ? "是" : "否");
    //性别
    borrowerDetailVO.setSex(borrower.getSex() == 1 ? "男" : "女");
    //审批状态
    borrowerDetailVO.setStatus(BorrowerStatusEnum.getMsgByStatus(borrower.getStatus()));

    // 计算下拉列表选中的内容
    String education = dictService
        .getNameByParentDictCodeAndValue("education", borrower.getEducation());
    String industry = dictService
        .getNameByParentDictCodeAndValue("industry", borrower.getIndustry());
    String income = dictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
    String returnSource = dictService
        .getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
    String contactsRelation = dictService
        .getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());

    borrowerDetailVO.setEducation(education);
    borrowerDetailVO.setIndustry(industry);
    borrowerDetailVO.setIncome(income);
    borrowerDetailVO.setReturnSource(returnSource);
    borrowerDetailVO.setContactsRelation(contactsRelation);

    // 获取附件VO列表
    List<BorrowerAttachVO> borrowerAttachVOList =  borrowerAttachService.selectBorrowerAttachVOList(id);
    borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVOList);
    return borrowerDetailVO;
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void approval(BorrowerApprovalVO borrowerApprovalVO) {

    //借款人认证状态
    Long borrowerId = borrowerApprovalVO.getBorrowerId();
    Borrower borrower = baseMapper.selectById(borrowerId);
    borrower.setStatus(borrowerApprovalVO.getStatus());
    baseMapper.updateById(borrower);

    Long userId = borrower.getUserId();
    UserInfo userInfo = userInfoMapper.selectById(userId);
    //添加积分
    UserIntegral userIntegral = new UserIntegral();
    userIntegral.setUserId(userId);
    userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
    userIntegral.setContent("借款人基本信息");
    userIntegralMapper.insert(userIntegral);

    int curIntegral = userInfo.getIntegral() + borrowerApprovalVO.getInfoIntegral();

    if (borrowerApprovalVO.getIsIdCardOk()) {
      curIntegral += IntegralEnum.BORROWER_IDCARD.getIntegral();
      userIntegral = new UserIntegral();
      userIntegral.setUserId(userId);
      userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
      userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
      userIntegralMapper.insert(userIntegral);
    }

    if (borrowerApprovalVO.getIsCarOk()) {
      curIntegral += IntegralEnum.BORROWER_CAR.getIntegral();
      userIntegral = new UserIntegral();
      userIntegral.setUserId(userId);
      userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
      userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
      userIntegralMapper.insert(userIntegral);
    }

    if (borrowerApprovalVO.getIsHouseOk()) {
      curIntegral += IntegralEnum.BORROWER_HOUSE.getIntegral();
      userIntegral = new UserIntegral();
      userIntegral.setUserId(userId);
      userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
      userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
      userIntegralMapper.insert(userIntegral);
    }
    //修改审核状态
    userInfo.setIntegral(curIntegral);
    userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
    userInfoMapper.updateById(userInfo);
  }

}
